<template>
  <div class="dashboard-container">
    <el-form label-width="120px">
      <el-form-item label="标题">
        <el-input v-model="banner.title"/>
      </el-form-item>
        <el-form-item label="图片">
            <el-upload
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                :action="BASE_API + '/ossservice/upload'"
                class="avatar-uploader">
                <img :src="banner.imageUrl">
            </el-upload>
        </el-form-item>
      <el-form-item label="链接">
        <el-input v-model="banner.linkUrl"/>
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="banner.sort" controls-position="right" :min="0"/>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import banner from '@/api/edu/banner'

export default {
    
    data() { // 定义变量和初始值
        return {
            banner: {
                imageUrl: '/static/01.jpg'
            },
            BASE_API:process.env.BASE_API, // 获取dev.env.js里面地址
            saveBtnDisabled: false // 保存按钮是否禁用
        }
    },
    created() { // 页面渲染之前执行，一般调用methods中定义的方法
        this.init()
    },
    watch: { // 监听
        $route(to, from) { // 路由发生变化，方法就会执行
            this.init()
        }
    },
    methods: { // 创建具体的方法，调用teacher.js中定义的方法
        init() {
            if (this.$route.params && this.$route.params.id) {
                const id = this.$route.params.id
                this.getBanner(id)
            } else {
                this.banner = {
                    imageUrl: '/static/01.jpg'
                }
            }
        },
        saveOrUpdate() {
            if (!this.banner.id) {
                this.saveBanner()
            } else {
                this.updateBanner()
            }
        },
        saveBanner() {
            banner.addBanner(this.banner).then(response => {
                this.$message({
                    type: 'success',
                    message: '添加成功！'
                })
            })
            this.$router.push({path: '/banner/table'})
        },
        getBanner(id) {
            banner.getBannerById(id).then(response => {
                this.banner = response.data.banner
            })
        },
        updateBanner() {
            banner.updateBanner(this.banner).then(response => {
                this.$message({
                    type: 'success',
                    message: '修改成功！'
                })
            })
            this.$router.push({path: '/banner/table'})
        },
        handleAvatarSuccess(res, file) {
            this.banner.imageUrl = res.data.url
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg'
            const isLt2M = file.size / 1024 / 1024 < 2

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!')
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!')
            }
            return isJPG && isLt2M
        }
    }
}
</script>
